<template>
  <section class="add">
    <el-form ref="ruleForm" :model="ruleForm" label-width="80px" :rules="rules" status-icon>
      <el-form-item label="姓名"  prop="teacherName">
            <el-input v-model="ruleForm.teacherName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="学院" prop="institute">
            <el-select v-model="ruleForm.institute" placeholder="请选择学院">
              <el-option :label="item.name" :value="item.name" v-for="item in collegeName" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-select v-model="ruleForm.sex" placeholder="请选择性别">
              <el-option label="男" value="男"></el-option>
              <el-option label="女" value="女"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="电话号码" prop="tel">
            <el-input v-model="ruleForm.tel"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pwd">
            <el-input v-model="ruleForm.pwd"></el-input>
          </el-form-item>
          <el-form-item label="身份证号" prop="cardId">
            <el-input v-model="ruleForm.cardId"></el-input>
          </el-form-item>
          <el-form-item label="职称" prop="type">
            <el-input v-model="ruleForm.type"></el-input>
          </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('ruleForm')" icon="el-icon-a-07">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </section>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: { //表单数据初始化
        teacherName: '',
        grade: '',
        major: '',
        clazz: null,
        institute: null,
        tel: null,
        email: null,
        pwd: null,
        cardId: null,
        sex: null,
        role: 2,
      },
      collegeName:[],
      // 表单校验规则
      rules:{
        teacherName:[
          {required:true,  trigger: 'blur',message:'姓名不能为空' },
        ],
        sex:[
          {required:true,message:'性别不能为空', trigger: 'blur' },
        ],
        institute:[
          {required:true, message:'学院不能为空',triggered:'change' },
        ],
        cardId:[
          {required:true,message:'身份证号不能为空', trigger: 'blur' },
        ],
        pwd:[
          {required:true,message:'密码不能为空', trigger: 'blur' },
        ],
        tel:[
          {required:true,message:'电话号码不能为空', trigger: 'blur' },
        ],
        type:[
          {required:true,message:'职称不能为空', trigger: 'blur' },
        ]
      }
    };
  },
  methods: {
    getCollege(){
      this.$axios(`/api/college/selectAllCollege/1/999`).then(res => {
        this.collegeName = res.data.data.records
      }).catch(error => {});
    },
    onSubmit(formName) { //数据提交
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios({
            url: '/api/teacher',
            method: 'post',
            data: {
              ...this.ruleForm
            }
          }).then(res => {
            if(res.data.code == 200) {
              this.$message({
                message: '数据添加成功',
                type: 'success'
              })
              this.$router.push({path: '/teacherManage'})
            }
          })
        } else {
          console.log('数据不合格');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.getCollege()
  }
};
</script>
<style lang="less" scoped>
.add {
  padding: 0 40px;
  width: 400px;
}
</style>

